﻿Imports System.Data.OleDb
Imports System.Net.Mail
Public Class Opstellen
    Inherits System.Web.UI.Page
    'Declaratie
    Dim strGeselecteerdeId() As String
    Dim intVergaderingId As Integer
    Dim intArrayItems As Integer = -1
    Dim intIdVergadering As Integer


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            'Declaratie
            Dim cnndb3 As New OleDbConnection
            Dim cmdCommando As New OleDbCommand
            Dim dtrDataReader As OleDbDataReader
            Dim strsqlEmailadress As String
            Dim strInListbox As String
            'SQL klaar zetten
            strsqlEmailadress = "SELECT [E-mail],[Id],[VoorNaam],[Naam] FROM [tblAandeelhouders]"
            'Connectie voorbereiden
            cnndb3.ConnectionString = My.Settings.csAandeelhouders
            cmdCommando.Connection = cnndb3
            cmdCommando.CommandText = strsqlEmailadress

            'Openen van database
            Try
                cnndb3.Open()
            Catch ex As Exception
                lblFout.Text = "Kan de database niet openen"
            End Try

            'Gegevens uit de database halen
            Try
                dtrDataReader = cmdCommando.ExecuteReader
                Do While dtrDataReader.Read()
                    'In mooie volgorde zetten zodat het met een goede layout in de checkboxlist kan
                    strInListbox = dtrDataReader.Item("Id") & " | "
                    strInListbox &= dtrDataReader.Item("Voornaam") & " "
                    strInListbox &= dtrDataReader.Item("Naam") & " | "
                    strInListbox &= dtrDataReader.Item("E-mail")

                    'In de checkboxlist steken de naam en de waarde
                    cblEmail.Items.Add(New ListItem(strInListbox, dtrDataReader.Item("Id")))
                Loop
            Catch ex As Exception
                lblFout.Text = "Kan de bewerking niet uitvoeren in de database"
            Finally
                'Connectie sluiten met de database
                cnndb3.Close()
            End Try
        End If

    End Sub
    Protected Sub btnSelecteerAlles_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSelecteerAlles.Click
        'Alle checkboxen van de checkboxlist aanvinken
        For intTeller As Integer = 0 To cblEmail.Items.Count - 1
            cblEmail.Items(intTeller).Selected = True
        Next
    End Sub

    Protected Sub btnDeselecteerall_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDeselecteerall.Click
        'Alle checkboxen van de checkboxlist afvinken
        For intTeller As Integer = 0 To cblEmail.Items.Count - 1
            cblEmail.Items(intTeller).Selected = False
        Next
    End Sub

    Protected Sub btnVoorbeeld_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnVoorbeeld.Click
        'Declaratie
        Dim intAantalAandeelhouders As Integer

        intIdVergadering = dpdVergadering.Text
        'Tellen hoeveel items in de checkboxlist zijn
        intAantalAandeelhouders = cblEmail.Items.Count


        If Not cblEmail.SelectedIndex = -1 Then


            'Na gaan welke gecheckt zijn en welke niet
            For intTeller As Integer = 0 To intAantalAandeelhouders - 1
                If cblEmail.Items(intTeller).Selected Then
                    intArrayItems += 1
                    ReDim Preserve strGeselecteerdeId(intArrayItems)
                    strGeselecteerdeId(intArrayItems) = cblEmail.Items(intTeller).Value
                End If
            Next


            Server.Transfer("Voorbeeld.aspx")
        Else
            lblFout.Text = "Gelieve een e-mail adress aan te vinken"

        End If


    End Sub
    Public ReadOnly Property GeselecteerdeId() As String()

        Get
            Return strGeselecteerdeId
        End Get

    End Property


    Public ReadOnly Property AantalGeselecteerd As Integer
        Get
            Return intArrayItems
        End Get
    End Property

    Public ReadOnly Property DefaultBrief As String
        Get
            Return mailContent.InnerHtml
        End Get
    End Property


    Public ReadOnly Property IdVergadering As String
        Get
            Return intIdVergadering
        End Get
    End Property

    Public ReadOnly Property TitelVergadering As String
        Get
            Return dpdVergadering.SelectedItem.ToString
        End Get
    End Property
End Class